var UNDEFINED = 'undefined';

var Method = function(methodName, isAuth) {
	var name = methodName;
	var params = [];
	var auth = isAuth;

	return {
		put : function(name) {
			params.push(name);
			return true;
		},
		url : function(values) {
			var methodUrl = null;

			methodUrl = "&method=" + name;
			params.each( function(param) {
				if (typeof values[param] !== UNDEFINED) {
					methodUrl += "&" + param + "=" + values[param];
				}
				return param;
			});

			return methodUrl;
		}
	}
}

var LastFM = function() {

	var API_METHODS = {};
	var API_KEY = "fadb5063e744d84f2382a2eb7d9dced8";
	var API_URL = "http://ws.audioscrobbler.com/2.0/";

	

	return {

		ARTIST_SEARCH : "artist.search",
		ARTIST_INFO: "artist.info",
		
		put : function(name, paramz, isAuth) {

			var method = new Method(name, isAuth);
			paramz.each( function(param) {
				method.put(param);
				return true;
			});

			API_METHODS[name] = method;

			return true;
		},

		get : function(name) {
			return API_METHODS[name];
		},

		url : function(methodName, params) {
			var uri = null;
			uri = API_URL + "?api_key=" + API_KEY;
			uri += API_METHODS[methodName].url(params);
			uri +="&noCache=" + (new Date().getTime());
			return uri;
		},

		init : function() {
			this.put(LastFM.ARTIST_SEARCH, [ "artist", "limit", "page" ], false);
			this.put(LastFM.ARTIST_INFO, [ "artist", "mbid", "lang" ], false);
			// alert(this.url(ARTIST_SEARCH,{ "artist":"Cher","limit":"1" }));
			// alert(this.url(ARTIST_INFO,{ "artist":"Cher","limit":"1" }));
		return null;
	}
	}

}();

Event.observe(window, 'load', LastFM.init.bind(LastFM));